package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.mapper.NoticeMapper;
import cn.tedu._03vehicle.pojo.datatransferobject.UserRegParam;
import cn.tedu._03vehicle.pojo.valueobject.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

@Tag(name = "03-公告模块")
@RestController
@RequestMapping("v1/notice")
@Slf4j
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @GetMapping("/list")
    @ApiOperationSupport(order = 10)
    @Operation(summary = "01-资讯模块")
    public JsonResult selectNoticeList() {
        Serializable noticeIndex = redisTemplate.opsForValue().get("notice_index");
        if (noticeIndex != null) {
            log.debug("从Redis中获取了公告索引：" + noticeIndex);
            return JsonResult.ok(noticeIndex);
        }
        //缓存中不存在数据
        List<NoticeListVO> noticeListVOS = noticeMapper.queryNotice();
        //缓存到Redis中30秒
        redisTemplate.opsForValue().set("notice_index", (Serializable) noticeListVOS, 30, TimeUnit.SECONDS);
        log.debug("从MySQL数据库中获取了公告索引：" + noticeListVOS);
        return JsonResult.ok(noticeListVOS);
    }
}
